const service = require('../service/label.service')

const verifyLabelExists = async (ctx, next) =>{
  const {labels} = ctx.request.body;
  // 判断每一个标签在label表里是否存在

  const newLabels = [];
  for(let name of labels){
    const labelResult = await service.getLabelByName(name);
    const label = {name};
    if(!labelResult){
      const result = await service.create(name);
      label.id = labelResult.id;
    }else{
      label.id = labelResult.id;
    }
    newLabels.push(label)
  }
  ctx.labels = newLabels
  await next();
}

module.exports = {
  verifyLabelExists 
}